package com.example.androidclass;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import org.litepal.tablemanager.Connector;

import java.sql.Connection;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.OffsetDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class AddActivity extends AppCompatActivity {

    private ImageView add_activity_close;
    private ImageView add_activity_save;
    private ImageView add_activity_delete;
    private EditText add_activity_title;
    private TextView add_activity_date;
    private TextView add_activity_start_time;
    private TextView add_activity_end_time;
    private EditText add_activity_place;
    private EditText add_activity_note;
    private Button create;
    private MyDate myDate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_activity);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        add_activity_close = findViewById(R.id.add_activity_close);
        add_activity_save = findViewById(R.id.add_activity_save);
        add_activity_delete = findViewById(R.id.add_activity_delete);
        add_activity_title =findViewById(R.id.add_activity_title);
        add_activity_date =findViewById(R.id.add_activity_date);
        add_activity_start_time =findViewById(R.id.add_activity_start_time);
        add_activity_end_time =findViewById(R.id.add_activity_end_time);
        add_activity_place =findViewById(R.id.add_activity_place);
        add_activity_note =findViewById(R.id.add_activity_note);
        create = findViewById(R.id.create_database);
        create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Connector.getDatabase();
            }
        });

        Calendar calendar = Calendar.getInstance();
        Log.d("Click", String.valueOf(calendar.get(Calendar.MONTH)));
        myDate = setDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

        add_activity_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddActivity.this, Timeline.class);
                startActivity(intent);
            }
        });

        add_activity_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view1) {
                Dialog dialog;
                AlertDialog.Builder builder = new AlertDialog.Builder(AddActivity.this);
                View view = getLayoutInflater().inflate(R.layout.dialog_date, null);
                final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);
                final Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                builder.setView(view);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        myDate = setDate(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                });
                builder.show();
            }
        });
    }


    public class MyDate {
        private int year;
        private int month;
        private int day_of_month;
        private String day_of_week;
        private MyDate(int year, int month, int day_of_month, String day_of_week) {
            this.year = year;
            this.month = month;
            this.day_of_month = day_of_month;
            this.day_of_week = day_of_week;
        }
        public int getYear() {
            return year;
        }

        public int getMonth() {
            return month;
        }

        public int getDay_of_month() {
            return day_of_month;
        }

        public String getDay_of_week() {
            return day_of_week;
        }
    }
//    public class MyTime {
//        private int hour;
//        private int minute;
//        private MyTime(int hour, int minute) {
//            this.hour = hour;
//            this.minute = minute;
//        }
//
//        public int getHour() {
//            return hour;
//        }
//
//        public int getMinute() {
//            return minute;
//        }
//    }

    private String  getDayOfWeek(String dateTime) {
        Calendar cal = Calendar.getInstance();
        if (dateTime.equals("")) {
            cal.setTime(new Date(System.currentTimeMillis()));
        } else {
            SimpleDateFormat simple_date_format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            Date date;
            try {
                date = simple_date_format.parse(dateTime);
            } catch (ParseException e) {
                date = null;
                e.printStackTrace();
            }
            if (date != null) {
                cal.setTime(new Date(date.getTime()));
            }
        }
        return convertDayOfWeek(cal.get(Calendar.DAY_OF_WEEK));
    }

    private String convertDayOfWeek(int day) {
        switch (day) {
            case 1:return "周日";
            case 2:return "周一";
            case 3:return "周二";
            case 4:return "周三";
            case 5:return "周四";
            case 6:return "周五";
            case 7:return "周六";
            default:return "";
        }
    }

    private MyDate setDate(int year, int month, int day_of_month){
        String format = String.valueOf(year) + "-" + String.valueOf(month + 1)  + "-" + String.valueOf(day_of_month);
        String day_of_week = getDayOfWeek(format);
        add_activity_date.setText(year + "年" + (month + 1) + "月" + day_of_month + "日" + day_of_week );
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(year, month, day_of_month);
        return new MyDate(year, month, day_of_month, day_of_week);
    }
}
